<template>
  <div style="display: inline-block" v-if="isMiddleRole()">
    <el-button type="primary" @click="showDialog">已送仓</el-button>
    <el-button type="danger" @click="cancel">作废</el-button>
    <el-dialog
      title="确认已送仓"
      :visible.sync="deliveryDialog"
      append-to-body
      width="500px"
    >
      <div class="font-red mgb10 font-bold" style="margin-top: -20px">请确认本次送仓是否收费</div>
      <el-form ref="form" :model="deliveryFrom" label-suffix="：" label-position="left" label-width="100px">
        <el-form-item label="是否收费">
          <el-radio-group v-model="deliveryFrom.isFee">
            <el-radio :label="false">不收费</el-radio>
            <el-radio :label="true">收费</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="送仓方式" v-if="deliveryFrom.isFee">
          <el-radio-group v-model="deliveryFrom.deliveryType">
            <el-radio label="general">常规</el-radio>
            <el-radio label="express">快递</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="btnLoading">确认</el-button>
          <el-button @click="deliveryDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {fullShipmentCancel, fullShipmentDelivery} from "@/api/shipmentV2";
import {isMiddleRole} from "@/utils/common";

export default {
  name: "ShipmentFullBtn",
  props: {
    data: {
      type: Object,
      required: true,
    },
    deliveryFrom: {
      type: Object,
      required: true
    },
  },
  data(){
    return {
      btnLoading: false,
      deliveryDialog: false,
    }
  },
  methods: {
    isMiddleRole,
    showDialog(){
      this.deliveryDialog = true
    },
    // 确认送仓
    submitForm(){
      this.deliveryFrom.id = this.data.id
      this.btnLoading = true
      fullShipmentDelivery(this.deliveryFrom)
        .then(res=>{
          this.btnLoading = false
          if(res.error === 0){
            this.deliveryDialog = false
            this.$emit('getData');
            this.$successMessage(res.message)
          }else{
            this.$errorMessage(res.message)
          }
        })
    },
    // 作废
    cancel(){
      new Promise((resolve, reject)=>{
        resolve()
      })
        .then(()=>{
          this.$confirm('确认作废当前货件单?')
            .then(()=>{
              fullShipmentCancel({
                id: this.data.id
              })
                .then(res=>{
                  if(res.error === 0){
                    this.$successMessage(res.message)
                    this.$emit('getData')
                  }else{
                    this.$errorMessage(res.message)
                  }
                })
            })
            .catch(()=>{})
        })
        .catch(()=>{})
    }
  }
}
</script>

<style scoped lang="scss">

</style>
